package com.gopi.remfilebrowser.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.gopi.remfilebrowser.gui.FileBrowserTreeNode;

public class RemoteFileBrowser 
{
	public static Set<FileBrowserTreeNode> getFiles(String host,String path) throws Exception
	{
		Set<FileBrowserTreeNode> list = new TreeSet<FileBrowserTreeNode>();
		URL url = new URL("http://" + host +
				"/projectmaker/browse/browse.do?dir=" + URLEncoder.encode(path,"UTF-8"));
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		if(con.getHeaderField("ERROR")==null)
		{
			BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String st;
			String info[];
			while((st=reader.readLine())!=null)
			{
				info = FieldParser.getField(st);
				FileBrowserTreeNode node = new FileBrowserTreeNode();
				node.setNodeType(FileBrowserTreeNode.getMappingInt(info[1]));
				node.setFileName(info[0]);
				list.add(node);
			}
		}
		else
		{
			con.disconnect();
			throw new Exception(con.getHeaderField("ERROR"));
		}
		con.disconnect();
		return list;
	}
	
	public static Thread getFilePropertyAsync(final String name,final  UpdateFilePropertyNotifier notifier)
	{
		Thread t = new Thread(new Runnable() {
			public void run()
			{
				FileProperty prop = getFileProperty(name, notifier);
				if(notifier!=null)
				{
					notifier.notifyUpdate(prop);
				}
			}
		});
		t.start();
		return t;
	}
	
	public static FileProperty getFileProperty(String name,UpdateFilePropertyNotifier notifier)
	{
		FileProperty property = new FileProperty();
		File f = new File(name);
		if(f.isFile())
		{
			property.setFileName(f.getName());
			property.setFilePath(f.getParent());
			property.setFileSize(f.length());
		}
		else if(f.isDirectory())
		{
			property.setFileName(f.getName());
			property.setFilePath(f.getParent());
			property.setFileSize(0L);
			property.setFile(false);
			getFilePropertyRecursive(name, property, notifier);
		}
		return property;
	}
	
	public static void getFilePropertyRecursive(String folderName,FileProperty property,UpdateFilePropertyNotifier notifier)
	{
		File f = new File(folderName);
		
		File[] files = f.listFiles();
		if(files==null)
			return;
		for(File file : files)
		{
			if(file.isFile())
			{
				property.setFileCount(property.getFileCount()+1);
				property.setFileSize(property.getFileSize() + file.length());
				getFilePropertyRecursive(file.getAbsolutePath(), property, notifier);
			}
			else 
			{
				property.setFolderCount(property.getFolderCount()+1);
				if(notifier!=null)
				{
					notifier.notifyUpdate(property);
				}
				getFilePropertyRecursive(file.getAbsolutePath(), property, notifier);
			}
		}
	}
	
	/*class FileSizeComputor implements Runnable
	{
		private 
		public void run()
		{
			
		}
	}*/
	
	static public interface UpdateFilePropertyNotifier
	{
		public void notifyUpdate(FileProperty property);
	}
	
	public static void main(String[] args) throws Exception
	{
		
		new Thread(new Runnable() {
			public void run()
			{
				FileProperty property = getFileProperty("D:\\Java\\Workspace",new UpdateFilePropertyNotifier() {
					public void notifyUpdate(FileProperty prop)
					{
						System.out.println(prop);		
					}
				});
			}
		}).start();
	}
}
